<?php

require_once 'FToolbarMenu.class.php';

class FStartMenu extends FToolbarMenu
{    
    
    public function __construct()
    {
        parent::__construct('startMenu');
        $this->setCls('start-button-icon');
        $this->setHeight('100%');
        $this->setWidth('5%');
        
        $xml = new FXMLEditor('start_menu.conf');
        $menu = $xml->getValue('menu');
        
        $this->gerarMenu($this, array_reverse($menu));
    }
    
    private function gerarMenu($menu, $menuArray, $subKey = NULL)
    {
        $menu instanceof FToolbarMenu;
        
        $keys = array_keys($menuArray);
        $attributes = array('model', 'text', 'image', 'table', 'function');
        
        foreach ( $keys as $key )
        {
            if ( !in_array($key, $attributes) && ($subKey == NULL || $key == $subKey) )
            {
                $menuItem = new FToolbarMenu("item_$key", $menuArray[$key]['text']);
                if ( strlen($menuArray[$key]['model']) > 0 )
                {
                    if ( class_exists($menuArray[$key]['model']) )
                    {
                        $model = new $menuArray[$key]['model']($menuArray[$key]['model']);
                        if ( strlen($menuArray[$key]['table']) > 0 )
                        {
                            $model->setControllerTable($menuArray[$key]['table']);
                        }
                        
                        if ( (strlen($menuArray[$key]['function']) > 0) )
                        {
                            $function = $menuArray[$key]['function'];
                            if ( method_exists($model, $function) )
                            {
                                $menuItem->setHandler("
                                    Ext.Ajax.request({
                                        url: 'src/php/handler.php',
                                        params: {
                                            action: 'submit',
                                            function: '{$function}',
                                            manager: '{$menuArray[$key]['model']}'
                                        },
                                        success: function(response) {
                                            obj = Ext.JSON.decode(response.responseText);
                                    
                                            if ( obj.confirmOnClose )
                                            {                                            
                                                Ext.Msg.confirm('Confirmação', '<b>' + obj.onCloseConfirmMsg + '</b>', function(btn)
                                                {
                                                    if( btn == 'yes' )
                                                    {
                                                        eval(obj.action);
                                                    }
                                                }, this);
                                            }
                                            else
                                            {
                                                eval(obj.action);
                                            }
                                        }
                                    });
                                ");
                            }
                            
                        }
                        else
                        {
                            $menuItem->setHandler($model->getForm());
                        }
                    }
                    else
                    {
                        $model = new FDefaultModel($menuArray[$key]['model']);
                        if ( strlen($menuArray[$key]['table']) > 0 )
                        {
                            $model->setControllerTable($menuArray[$key]['table']);
                        }
                        $menuItem->setHandler($model->getForm());
                    }
                }

                foreach ( $menuArray[$key] as $sbKey => $subItem )
                {
                    if ( !in_array($sbKey, $attributes) )
                    {
                        $menuItem->addItem($this->gerarMenu($menuItem, $menuArray[$key], $sbKey));
                    }
                }

                $menu->addItem($menuItem);
            }
        }
    }
    
}

?>
